package org.kl.auditsupervision.service.fnd;

import java.sql.Timestamp;

import org.activiti.engine.delegate.DelegateExecution;
import org.apache.commons.lang3.StringUtils;
import org.joda.time.DateTime;
import org.kl.auditsupervision.entity.fnd.AuditPro;
import org.kl.auditsupervision.entity.fnd.AuditProDtl;
import org.kl.auditsupervision.repository.fnd.AuditProDao;
import org.kl.auditsupervision.repository.fnd.AuditProDtlDao;
import org.kl.bf.service.BasicService;
import org.kl.bf.utils.BasicConstants;
import org.kl.bf.web.base.BasicController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;


@Component
//默认将类中的所有public函数纳入事务管理.
@Transactional
public class AuditService extends BasicService<AuditPro>{
	@Autowired
	  private AuditProDao auditProDao;
	@Autowired
	private AuditProDtlDao auditProDtlDao;
	
	//测试用，最终所有方法转移到AuditProService
	/**
	 * 流程正常结束
	 * 
	 * @param execution
	 * @param authenticatedUserId
	 */
	public void wflComplete(DelegateExecution execution,
			String authenticatedUserId) {
		String businessKey = execution.getProcessBusinessKey();
		if (StringUtils.isNotBlank(businessKey)) {
			AuditProDtl rpa = auditProDtlDao.getOne(businessKey);
			rpa.setConfirmFlag(BasicConstants.CONFIRM_STATUS_Confirm);
			rpa.setConfirmMan(BasicController.getCurrentUserName());
			rpa.setConfirmTime(new Timestamp(new DateTime().getMillis()));
			auditProDtlDao.save(rpa);
		}
	}
	
	/**
	 * 发起人同意流程
	 * 
	 * @param execution
	 * @param authenticatedUserId
	 */
	public void wflAgree(DelegateExecution execution, String authenticatedUserId) {
		String businessKey = execution.getProcessBusinessKey();
		if (StringUtils.isNotBlank(businessKey)) {
			AuditProDtl rpa = auditProDtlDao.getOne(businessKey);
			rpa.setConfirmFlag(BasicConstants.CONFIRM_STATUS_Approving);
			auditProDtlDao.save(rpa);
		}
	}

	/**
	 * 流程不同意
	 * 
	 * @param execution
	 * @param authenticatedUserId
	 */
	public void wflDisagree(DelegateExecution execution,
			String authenticatedUserId) {
		String businessKey = execution.getProcessBusinessKey();
		if (StringUtils.isNotBlank(businessKey)) {
			AuditProDtl rpa = auditProDtlDao.getOne(businessKey);
			rpa.setConfirmFlag(BasicConstants.CONFIRM_STATUS_Draft);
			rpa.setConfirmMan(BasicController.getCurrentUserName());
			rpa.setConfirmTime(new Timestamp(new DateTime().getMillis()));
			auditProDtlDao.save(rpa);
		}
	}

	/**
	 * 流程取消
	 * 
	 * @param execution
	 * @param authenticatedUserId
	 */
	public void wflCancel(DelegateExecution execution,
			String authenticatedUserId) {
		String businessKey = execution.getProcessBusinessKey();
		if (StringUtils.isNotBlank(businessKey)) {
			AuditProDtl rpa = auditProDtlDao.getOne(businessKey);
			rpa.setConfirmFlag(BasicConstants.CONFIRM_STATUS_Cancel);
			rpa.setConfirmMan(BasicController.getCurrentUserName());
			rpa.setConfirmTime(new Timestamp(new DateTime().getMillis()));
			auditProDtlDao.save(rpa);
		}
	}
}
